package m1_simple;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class Producer {
//    static Channel c;
//    static Connection connection;
//    static {//连接 - 通道
//        ConnectionFactory f = new ConnectionFactory();
//        f.setHost("192.168.64.140");
//        f.setPort(5672);
//        f.setUsername("admin");
//        f.setPassword("admin");
//
//        try {
//            connection = f.newConnection();
//            c = connection.createChannel();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//    }

    public static void main(String[] args) throws Exception {
        //1.连接
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");
        Connection con = f.newConnection();
        Channel c = con.createChannel();
        //2.定义队列    告诉服务器我们要使用的队列是那个队列
        //如果已经存在直接使用 如果不存在 mq会新建队列
        c.queueDeclare(
                "helloworld",
                false,
                false,
                false,
                null
        );
        //3.发送消息
        c.basicPublish("",
                "helloworld",
                null,
                "hello world".getBytes());
        System.out.println("消息已发送");
        //4.断开连接
        con.close();
    }
}
